package com.zhangh.summer.admin.support.prometheus.druid;

import com.alibaba.druid.pool.DruidDataSource;
import com.zhangh.summer.admin.support.multipleds.DynamicDataSource;
import com.zhangh.summer.admin.support.spring.util.SpringBeanUtil;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: 连接池收集器
 * @author: zhanghui  zhanghlucky@163.com
 * @create: 2020-06-29 16:09
 **/
@Component
public class DruidCollector {
    private List<DruidDataSource> dataSources=new ArrayList<>();
    private DynamicDataSource dynamicDataSource;

    @PostConstruct
    public void init(){
        dynamicDataSource= SpringBeanUtil.getBeanByType(DynamicDataSource.class);
    }

    public void execute(){
        if (null!=dynamicDataSource){
            refresh();
        }
    }

    private void refresh() {
        dataSources.addAll(dynamicDataSource.getDruidDatasourceMap().values());
        this.dataSources.forEach((druidDataSource) -> {
            String druidName = druidDataSource.getUrl().split("\\?")[0].replaceAll("jdbc:mysql://", "").replaceAll("/", "");
            ((io.prometheus.client.Gauge.Child)DruidMetrics.druidInitialSizeEndpoint.labels(new String[]{druidName})).set((double)druidDataSource.getInitialSize());
            ((io.prometheus.client.Gauge.Child)DruidMetrics.druidMinIdleEndpoint.labels(new String[]{druidName})).set((double)druidDataSource.getMinIdle());
            ((io.prometheus.client.Gauge.Child)DruidMetrics.druidMaxActiveEndpoint.labels(new String[]{druidName})).set((double)druidDataSource.getMaxActive());
            ((io.prometheus.client.Gauge.Child)DruidMetrics.druidActiveCountEndpoint.labels(new String[]{druidName})).set((double)druidDataSource.getActiveCount());
            ((io.prometheus.client.Gauge.Child)DruidMetrics.druidActivePeakEndpoint.labels(new String[]{druidName})).set((double)druidDataSource.getActivePeak());
            ((io.prometheus.client.Gauge.Child)DruidMetrics.druidPoolingPeakEndpoint.labels(new String[]{druidName})).set((double)druidDataSource.getPoolingPeak());
            ((io.prometheus.client.Gauge.Child)DruidMetrics.druidPoolingCountEndpoint.labels(new String[]{druidName})).set((double)druidDataSource.getPoolingCount());
            ((io.prometheus.client.Gauge.Child)DruidMetrics.druidWaitThreadCountEndpoint.labels(new String[]{druidName})).set((double)druidDataSource.getWaitThreadCount());
            ((io.prometheus.client.Gauge.Child)DruidMetrics.druidNotEmptyWaitCountEndpoint.labels(new String[]{druidName})).set((double)druidDataSource.getNotEmptyWaitCount());
            ((io.prometheus.client.Gauge.Child)DruidMetrics.druidNotEmptyWaitMillisEndpoint.labels(new String[]{druidName})).set((double)druidDataSource.getNotEmptyWaitMillis());
            ((io.prometheus.client.Gauge.Child)DruidMetrics.druidNotEmptyThreadCountEndpoint.labels(new String[]{druidName})).set((double)druidDataSource.getNotEmptyWaitThreadCount());
            ((io.prometheus.client.Gauge.Child)DruidMetrics.druidLogicConnectCountEndpoint.labels(new String[]{druidName})).set((double)druidDataSource.getConnectCount());
            ((io.prometheus.client.Gauge.Child)DruidMetrics.druidLogicCloseCountEndpoint.labels(new String[]{druidName})).set((double)druidDataSource.getCloseCount());
            ((io.prometheus.client.Gauge.Child)DruidMetrics.druidLogicConnectErrorCountEndpoint.labels(new String[]{druidName})).set((double)druidDataSource.getConnectErrorCount());
            ((io.prometheus.client.Gauge.Child)DruidMetrics.druidPhysicalConnectCountEndpoint.labels(new String[]{druidName})).set((double)druidDataSource.getCreateCount());
            ((io.prometheus.client.Gauge.Child)DruidMetrics.druidPhysicalCloseCountEndpoint.labels(new String[]{druidName})).set((double)druidDataSource.getDestroyCount());
            ((io.prometheus.client.Gauge.Child)DruidMetrics.druidPhysicalConnectErrorCountEndpoint.labels(new String[]{druidName})).set((double)druidDataSource.getCreateErrorCount());
            ((io.prometheus.client.Gauge.Child)DruidMetrics.druidErrorCountEndpoint.labels(new String[]{druidName})).set((double)druidDataSource.getErrorCount());
            ((io.prometheus.client.Gauge.Child)DruidMetrics.druidExecuteCountEndpoint.labels(new String[]{druidName})).set((double)druidDataSource.getExecuteCount());
            ((io.prometheus.client.Gauge.Child)DruidMetrics.druidStartTransactionCountEndpoint.labels(new String[]{druidName})).set((double)druidDataSource.getStartTransactionCount());
            ((io.prometheus.client.Gauge.Child)DruidMetrics.druidCommitCountEndpoint.labels(new String[]{druidName})).set((double)druidDataSource.getCommitCount());
            ((io.prometheus.client.Gauge.Child)DruidMetrics.druidRollbackCountEndpoint.labels(new String[]{druidName})).set((double)druidDataSource.getRollbackCount());
            ((io.prometheus.client.Gauge.Child)DruidMetrics.druidPreparedStatementOpenCountEndpoint.labels(new String[]{druidName})).set((double)druidDataSource.getPreparedStatementCount());
            ((io.prometheus.client.Gauge.Child)DruidMetrics.druidPreparedStatementClosedCountEndpoint.labels(new String[]{druidName})).set((double)druidDataSource.getClosedPreparedStatementCount());
            ((io.prometheus.client.Gauge.Child)DruidMetrics.druidPsCacheAccessCountEndpoint.labels(new String[]{druidName})).set((double)druidDataSource.getCachedPreparedStatementAccessCount());
            ((io.prometheus.client.Gauge.Child)DruidMetrics.druidPsCacheHitCountEndpoint.labels(new String[]{druidName})).set((double)druidDataSource.getCachedPreparedStatementHitCount());
            ((io.prometheus.client.Gauge.Child)DruidMetrics.druidPsCacheMissCountEndpoint.labels(new String[]{druidName})).set((double)druidDataSource.getCachedPreparedStatementMissCount());
            ((io.prometheus.client.Gauge.Child)DruidMetrics.druidMaxWaitEndpoint.labels(new String[]{druidName})).set((double)druidDataSource.getMaxWait());
            ((io.prometheus.client.Gauge.Child)DruidMetrics.druidMaxWaitThreadCountEndpoint.labels(new String[]{druidName})).set((double)druidDataSource.getMaxWaitThreadCount());
            ((io.prometheus.client.Gauge.Child)DruidMetrics.druidLoginTimeoutEndpoint.labels(new String[]{druidName})).set((double)druidDataSource.getLoginTimeout());
            ((io.prometheus.client.Gauge.Child)DruidMetrics.druidQueryTimeoutEndpoint.labels(new String[]{druidName})).set((double)druidDataSource.getQueryTimeout());
            ((io.prometheus.client.Gauge.Child)DruidMetrics.druidTransactionQueryTimeoutEndpoint.labels(new String[]{druidName})).set((double)druidDataSource.getTransactionQueryTimeout());
        });
    }


}
